
var attrList = new Array();
var attrObjList = {};

//更新串口信息
function updateSerialPo(indix) {
    showUSPModol();
    showUPSModolFoot("update");
    var tdList = $("#setUpSerialPo_tbody").children().get(indix).children;
    for (var i = 0; i < tdList.length-1; i++) {
        var name = $(tdList[i]).attr("name");
        var value = $(tdList[i]).children().get(0).innerText;
        var object = $("#modal_" + name + "");
        var localName = object.get(0).localName;
        attrList[i] = name;
        switch (localName) {
            case "select":{
                object.find("option:contains(" + value + ")").attr("selected", true);;
                attrObjList[name] = object;
                continue;
            }
            case "div":{
                $("input[type=radio][name=modal_" + name + "][value=" + value + "]").attr("checked", "checked");
                attrObjList[name] = $("<div></div>");
                continue;
            }
            case "input":{
                object.val(value);
                attrObjList[name] = object;
                continue;
            }
        }
    }
}

function getUSPFormInfo() {

}

//添加串口信息
function insertSerialPO() {
    showUSPModol();
    showUPSModolFoot("insert");
}

//获得modal中的数据
function updateUSPFormData(url) {
    var attrData = {};
    try {
        attrData = getData();
    }catch (e) {
        alert("请勿重复提交")
        return;
    }
    console.log(attrData)
    $.ajax({
        url:"/serial/" + url,
        type:"post",
        dataType:"json",
        data:attrData,
    });

    function getData() {
        var attrData = {};
        for (var i = 0; i < attrList.length ; i++) {
            var name = attrList[i];
            var object = attrObjList[name];
            switch (object.get(0).localName) {
                case "select":{
                    attrData['parmCN'] = $("#modal_" + name + "").find("option:selected").text();
                    attrData[name] = $("#modal_" + name + "").val()
                    continue;
                }
                case "div":{
                    attrData[name] = $("input:radio[name='modal_" + name + "']:checked").val();
                    continue;
                }
                case "input":{
                    attrData[name] = $("#modal_" + name + "").val();
                    continue;
                }
            }
        }
        return attrData;
    }
}

//加载模态框脚
function showUPSModolFoot(url) {
    $("#USPModalFoot").empty();
    $("#USPModalFoot").append("" +
        "<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">关闭</button>\n" +
        "<button type=\"button\" class=\"btn btn-warning\" onclick=\"updateUSPFormData(\'" +url + "\')\" >提交更改</button>")
}



//加载模态框框体
function showUSPModol() {
    $("#USPModalBody").empty();
    $("#USPModalBody").append("" +
        "<p class=\"card-description\">\n" +
        "                    请经过核对后，慎重填写！\n" +
        "                </p>\n" +
        "                <div class=\"form-group\" style=\"margin-bottom: 15px\">\n" +
        "                    <div class=\"input-group\">\n" +
        "                        <div class=\"input-group-prepend\" >\n" +
        "                            <select id=\"modal_parm\" class=\"magic btn btn-inverse-secondary \" style=\"border-radius: 10px 0px 0px 10px\"  onchange=\"\">\n" +
        "                                <option value=\"sulfurDioxide\">二氧化硫浓度</option>\n" +
        "                                <option value=\"nitrogenOxide\">氮氧化物浓度</option>\n" +
        "                                <option value=\"particulate\">颗粒物浓度</option>\n" +
        "                                <option value=\"oxygenContent\">含氧量</option>\n" +
        "                                <option value=\"temperature\">温度</option>\n" +
        "                                <option value=\"humidity\">湿度</option>\n" +
        "                                <option value=\"flow\">流量</option>\n" +
        "                            </select>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-check form-check-primary  btn-inverse-secondary col-md-6\" style=\"padding-right: 0px;margin: 0px;border-radius: 0px 10px 10px 0px\">\n" +
        "                        <span class=\"col-md-6\" style=\"padding: 0px 0px 0px 20px\">经过处理</span>\n" +
        "                        <div id=\"modal_ishandled\" class=\"form-inline col-md-6\" style=\"padding: 15px 0px 0px 5px; height: 54px\">\n" +
        "                            <label style=\"color: black;margin-right: 15px\"><input type=\"radio\" name=\"modal_ishandled\" class=\"form-check-input\" value=\"true\">是</label>\n" +
        "                            <label style=\"color: black\"><input type=\"radio\" name=\"modal_ishandled\" class=\"form-check-input\" value=\"false\">否</label>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "                <div class=\"form-group\" style=\"margin-bottom: 15px\">\n" +
        "                    <div class=\"input-group\" style=\"width: 400px\">\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text bg-primary text-white\">波特率</span>\n" +
        "                        </div>\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text\" style=\"width: 60px;padding-left:25px\">B</span>\n" +
        "                        </div>\n" +
        "                        <input id=\"modal_baudRate\"  type=\"text\" class=\"form-control\" aria-label=\"Amount (to the nearest dollar)\">\n" +
        "                        <div class=\"input-group-append\">\n" +
        "                            <span class=\"input-group-text\">波特</span>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "                <div class=\"form-group\" style=\"margin-bottom: 15px\">\n" +
        "                    <div class=\"input-group\" style=\"width: 400px\">\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text bg-primary text-white\">串口号</span>\n" +
        "                        </div>\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text\" style=\"width: 60px;padding-left:20px\">第</span>\n" +
        "                        </div>\n" +
        "                        <input id=\"modal_serialNumber\" type=\"text\" class=\"form-control\" aria-label=\"Amount (to the nearest dollar)\">\n" +
        "                        <div class=\"input-group-append\">\n" +
        "                            <span class=\"input-group-text\" >号</span>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "                <div class=\"form-group\" style=\"margin-bottom: 15px\">\n" +
        "                    <div class=\"input-group\" style=\"width: 400px\">\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text bg-primary text-white\">校验位</span>\n" +
        "                        </div>\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text\" style=\"width: 60px;padding-left:20px\">bit</span>\n" +
        "                        </div>\n" +
        "                        <input  id=\"modal_checkoutBit\" type=\"text\" class=\"form-control\" aria-label=\"Amount (to the nearest dollar)\">\n" +
        "                        <div class=\"input-group-append\">\n" +
        "                            <span class=\"input-group-text\">位</span>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "                <div class=\"form-group\" style=\"margin-bottom: 15px\">\n" +
        "                    <div class=\"input-group\" style=\"width: 400px\">\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text bg-primary text-white\">数据位</span>\n" +
        "                        </div>\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text\" style=\"width: 60px;padding-left:20px\">bit</span>\n" +
        "                        </div>\n" +
        "                        <input id=\"modal_dataBit\" type=\"text\" class=\"form-control\" aria-label=\"Amount (to the nearest dollar)\">\n" +
        "                        <div class=\"input-group-append\">\n" +
        "                            <span class=\"input-group-text\">位</span>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "                <div class=\"form-group\" style=\"margin-bottom: 15px\">\n" +
        "                    <div class=\"input-group\" style=\"width: 400px\">\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text bg-primary text-white\">停止位</span>\n" +
        "                        </div>\n" +
        "                        <div class=\"input-group-prepend\">\n" +
        "                            <span class=\"input-group-text\" style=\"width: 60px;padding-left:20px\">bit</span>\n" +
        "                        </div>\n" +
        "                        <input id=\"modal_stopBit\" name=\"stopBit\" type=\"text\" class=\"form-control\" aria-label=\"Amount (to the nearest dollar)\">\n" +
        "                        <div class=\"input-group-append\">\n" +
        "                            <span class=\"input-group-text\">位</span>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>" +
        "");
    $('#USP-Button').click();
}
